<?php
class Common
{
	/**
	 * Checking magic quotes settings and prepare GPRC data
	 */
	public static function checkMagicQuotes()
	{
		if (get_magic_quotes_gpc()) {
			if (!empty($_GET)) $_GET = self::stripMagicQuotes($_GET);
			if (!empty($_POST)) $_POST = self::stripMagicQuotes($_POST);
			if (!empty($_REQUEST)) $_REQUEST = self::stripMagicQuotes($_REQUEST);
			if (!empty($_COOKIE)) $_COOKIE = self::stripMagicQuotes($_COOKIE);
		}
	}
	
	//strip slashes from array value
	private static function stripMagicQuotes($arr)
	{
		foreach ($arr as $k => $v) {
			$arr[$k] = is_array($v) ? stripMagicQuotes($v) : stripslashes($v);
		}
		return $arr;
	}
	
	//fill form with input array
	public static function fillForm($array)
	{
		if(isset($array["location"]) == false)
		{
			return;
		}
		$js = "<script type=\"text/javascript\">
		var lo = '".$array["location"]."';
		var tpl = '".$array["template"]."';
		var chkEmail = '".$array["chkEmail"]."';
		if(lo == \"LA\")
		{
			document.getElementById('loSF').checked = false;
			document.getElementById('loLA').checked = true;
		}
		document.getElementById(tpl).checked = true;
		if(chkEmail != \"\" )
		{
			document.getElementById('chkEmail').checked = true;
		}
		else
		{
			document.getElementById('chkEmail').checked = false;
		}
		showEmailBox();
		</script>";
		echo $js;
	}
	
	/**
	 * Increase the number of transaction.
	 *
	 * @return Number of transaction.
	 */
	public static function countTransaction()
	{		
		//open file.
		$myFile = SYS_PATH."gift_cert/data/config/global.txt";
		$fh = fopen($myFile, 'r+');
		
		//read count variable from file.
		$count = fread($fh, 4);
		fseek($fh, SEEK_SET);
		
		//increase count.
		$count++;
		
		//write to file.
		fwrite($fh, $count);
		
		//close file.
		fclose($fh);
		return $count;
	}
	
	/**
	 * Split string by length
	 *
	 * @param string $string
	 * @param int $chunkLength
	 * @return array of string
	 */
	public static function splitByLength($string, $chunkLength=1)
	{
		$rs = array();
		$newtext = wordwrap($string, $chunkLength, "\r\n",1);
		$rs = split("\r\n",$newtext);
		return $rs;	
	}
	
	/**
	 * Get full url
	 *
	 * @return string
	 */
	public static function getAddress()
	 {
	    /*** check for https ***/
	    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
	    /*** return the full address ***/
	    $url = $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	    $pos = strrpos($url, "/");
	    return substr($url,0,$pos) . "/";
	 }
	
}
?>